// Package entry
// @Author: zhangdi
// @File: callback_hotkey
// @Version: 1.0.0
// @Date: 2023/5/30 17:49
package entry

import (
	"fmt"
	"ideatools/application/constant"
	"ideatools/application/constant/enums"
	"ideatools/application/core/events"
	"ideatools/application/db/dbservice"
	"strings"
)

func CallBackKeyboardClick(codes []uint16) {
	if notifyFrontKeyboard(codes) {
		//监听中，不做任何操作
		return
	}
	CombinedKey := "" //快捷键id
	for _, code := range codes {
		CombinedKey += fmt.Sprintf("%v+", code)
	}
	CombinedKey = strings.TrimRight(CombinedKey, "+")

	//检测配置文件里的
	CheckConfigHotKeyTouch(CombinedKey)

	//2.快捷键--快速触发tool
	info, err := UserToolService.GetUserToolByHotKey(CombinedKey)
	if err != nil {
		return
	}
	if info.UseHotKey {
		//如何该tool启用了快捷键，则调用处理函数
		HandlerMessageService.Send(info.Id, 0)
	}
}

func CallBackOneKeyClick(code uint16) {
	if code == enums.KeyCode_Escape {
		//	HideApp() //隐藏
	}
}

func CheckConfigHotKeyTouch(CombinedKey string) {
	//1.快捷键 快速启动程序
	switch CombinedKey {
	case ConfigService.Get(constant.ConfigKeyHotWinQuick):
		//切换快捷面板
		events.OnChangeWindow(enums.WindowsTypeQuickTools)
		break
	case ConfigService.Get(constant.ConfigKeyHotWinMain):
		events.OnChangeWindow(enums.WindowsTypeMain)

		break
	case ConfigService.Get(constant.ConfigKeyHotWinChat):
		events.OnChangeWindow(enums.WindowsTypeMessage)

		break
	case ConfigService.Get(constant.ConfigKeyHotNewChat):
		events.OnNewChat(false)
		break
	case ConfigService.Get(constant.ConfigKeyHotNewChatSelect): //新对话 选中文本,todo
		events.OnNewChat(false)
		break
	}

}

func notifyFrontKeyboard(codes []uint16) bool {
	if dbservice.Conf().GetBool(constant.ConfigKeyFrontIsListenKeyboard) {
		NotifyKeyBoardEvent(codes) //通知给前端
		return true
	}
	return false
}
